<?php
namespace app\common\validate\tysj;

class ProductSpecs extends Base {
    /**
     * 验证规则
     */
    protected $rule = [
        'lock'                  => 'require',
        'id'                    => 'require|isUser:tysj\\ProductSpecs',
        'uid'                   => 'require',
        'name'                  => 'require|max:32',
        //'crowdid'               => 'require|isOptions:Crowd',
        'price'                 => 'require|float|egt:0',
        'commission'            => 'require|float|egt:0|commission',
        'inventory'             => 'require|integer|gt:0',
        'inventory_warning'     => 'require|integer|gt:0|inventory_warning',
        'min_purchase_quantity' => 'require|integer|gt:0',
        'weight'                => 'float|gt:0',
        //'volume'                => 'require'
    ];


    /**
     * 提示消息
     */
    protected $message = [
        'lock.require'                        => '验证规则场景不存在',
        'id.require'                          => '产品规格ID不能为空',
        'id.isUser'                           => '产品规格不属于您',
        'id.isUsed'                           => '该规格已经被使用',
        'uid.require'                         => '用户ID不能为空',
        'name.require'                        => '产品规则名称不能为空',
        'name.max'                            => '产品规则名称超过32个字符',
        'crowdid.require'                     => '所属人群ID不能为空',
        'crowdid.isOptions'                   => '所属人群ID不合法',
        'price.require'                       => '价格不能为空',
        'price.float'                         => '价格不合法',
        'price.egt'                           => '价格必须大于等于0',
        'commission.require'                  => '佣金不能为空',
        'commission.float'                    => '佣金不合法',
        'commission.egt'                      => '佣金必须大于等于0',
        'commission.commission'               => '佣金不能超过价格的30%',
        'inventory.require'                   => '库存不能为空',
        'inventory.integer'                   => '库存不合法',
        'inventory.gt'                        => '库存必须大于0',
        'inventory_warning.require'           => '库存警告不能为空',
        'inventory_warning.integer'           => '库存警告必须是一个数字',
        'inventory_warning.gt'                => '库存警告必须大于0',
        'inventory_warning.inventory_warning' => '库存警告必须小于库存',
        'min_purchase_quantity.require'       => '最小购买数量不能为空',
        'min_purchase_quantity.integer'       => '最小购买数量不合法',
        'min_purchase_quantity.gt'            => '最小购买数量必须大于0',
        'weight.require'                      => '重量不能为空',
        'weight.float'                        => '重量不合法',
        'weight.gt'                           => '重量必须大于0',
        'volume.require'                      => '体积不能为空'
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'used'       => ['id'],
        'save'       => [
            'id'     => 'isUser:tysj\\ProductSpecs',
            'uid',
            'name',
            'crowdid',
            'price',
            'commission',
            'inventory',
            'inventory_warning',
            'min_purchase_quantity',
            'min_purchase_quantity',
            'weight',
            'volume'
        ],
        'private'    => ['uid'],
        'info'       => ['uid'],
        'del'        => ['id' => 'require|isUser:tysj\\ProductSpecs|isUsed', 'uid']
    ];

    protected function commission($value, $rule, $data = []) {
        if($value > $data['price'] * 0.3) {
            return false;
        }
        return true;
    }

    protected function inventory_warning($value, $rule, $data = []) {
        if($value >= $data['inventory']) {
            return false;
        }
        return true;
    }

    protected function isUsed($value, $rule, $data = []) {
        $productSpecsNexus = loadModel('tysj\\ProductSpecsNexus');
        $count = $productSpecsNexus->where(['psid' => $value])->count();
        if($count > 0) {
            return false;
        }
        return true;
    }


}